package com.mantisframework.core.spring.factorybean;

import java.util.Properties;

import jodd.util.StringUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.FactoryBean;

/**
 * jdbc密码加密串解密bean
 * @author mayitengstudio
 */
public class PropertiesEncryptFactoryBean implements FactoryBean {
	private static final Log log=LogFactory.getLog(PropertiesEncryptFactoryBean.class);
	private Properties properties;
	private String userkey;
    private String passwordkey;
    private static  final String USER="user";
    private static  final String PWD="password";
    
	public Object getObject() throws Exception {
	return getProperties();
	}

	public Class<Properties> getObjectType() {
	return java.util.Properties.class;
	}

	public boolean isSingleton() {
	return true;
	}

	public Properties getProperties() {
	return properties;
	}

	public void setProperties(Properties inProperties) { 
	this.properties = inProperties;
	String username = properties.getProperty(getUserkey());
	String pwd = properties.getProperty(getPasswordkey());
	
	if(StringUtil.isEmpty(username)){
		log.error("jdbc user load error");
		username="";
	}
	if(StringUtil.isEmpty(pwd)){
		log.error("jdbc password load error");
		pwd="";
	}
	System.out.println(username+"---"+pwd);
	 properties.put(userkey, username);
	 properties.put(passwordkey, pwd);

	}
	public void setUserkey(String userkey) {
		this.userkey = userkey;
	}
	public void setPasswordkey(String passwordkey) {
		this.passwordkey = passwordkey;
	}
	private String getUserkey() {
		if(userkey==null){
			return USER;
		}else{
			return userkey;
		}
	}
	private String getPasswordkey() {
		if(passwordkey==null){
		    return PWD;	
		}else{
			return passwordkey;
		}
	}


}
